package com.amigo.online.consumer.video.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.amigo.online.consumer.video.service.FileUploadService;
import com.amigo.online.consumer.video.task.SingleFileUploadCallable;
import com.amigo.online.consumer.video.util.UploadImgUtil;

@Service
public class FileUploadServiceImpl implements FileUploadService {

	protected static final Logger logger = LoggerFactory.getLogger(FileUploadServiceImpl.class);

	@Autowired
	ExecutorService executorService;

	@Override
	public Map<Integer, String> handleMultiFileUpload(MultipartFile[] files) {
		Map<Integer, String> resultMap = new HashMap<>(10);
		for (int i = 0; i < files.length; i++) {
			Callable<String> Callable = new SingleFileUploadCallable(files[i]);
			Future<String> futureResult = executorService.submit(Callable);
			try {
				resultMap.put(i, futureResult.get());
			} catch (InterruptedException | ExecutionException e) {
				logger.info("异步操作获取结果值异常！异常信息为:{}", e);
			}
		}
		return resultMap;
	}
	
	@Override
	public List<String> handleFileUploadList(MultipartFile[] files) {
		List<String> resultList = new ArrayList<>();
		List<Future<String>> futureList = new ArrayList<>();
		for (MultipartFile file : files) {
			Callable<String> callable = new SingleFileUploadCallable(file);
			Future<String> futureResult = executorService.submit(callable);
			futureList.add(futureResult);
		}
		//long current = System.currentTimeMillis();
		for(Future<String> future : futureList) {	
			try {
				String result = future.get();
				if(result == null) {
					result = "";
				}
				resultList.add(result);
			} catch (InterruptedException | ExecutionException e) {
				logger.info("异步操作获取结果值异常！异常信息为:{}", e);
			}			
		}
		//logger.info("---获取任务结果方式future,时间差为:{}", System.currentTimeMillis() - current);
		return resultList;
	}

	@Override
	public List<String> handleMultiFileUploadList(MultipartFile[] files) {
		List<String> resultList = new ArrayList<>(10);		
		if(files.length == 1) {
			handleSingleFileUpload(files[0], resultList);
		} else if (files.length > 1) {
			handleMutltiFileUpload(files, resultList);
		}
		
		return resultList;
	}

	private void handleMutltiFileUpload(MultipartFile[] files, List<String>  resultList) {
		List<FutureTask<String>> futureList = new ArrayList<>(10);
		for (MultipartFile file : files) {
			Callable<String> callable = new SingleFileUploadCallable(file);
			FutureTask<String> futureTask = new FutureTask<String>(callable);
			executorService.submit(futureTask);
			futureList.add(futureTask);
		}
		//long current = System.currentTimeMillis();	
		try {
			while (futureList.size() > 0) {
				Iterator<FutureTask<String>> iterable = futureList.iterator();
				while (iterable.hasNext()) {
					Future<String> future = iterable.next();
					if (future.isDone() && !future.isCancelled()) {
						String result = future.get();
						if(result == null) {
							result = "";
						}
						resultList.add(result);
						iterable.remove();
					}
				}
			}
		} catch (InterruptedException | ExecutionException e) {
			logger.info("异步操作获取结果值异常！异常信息为:{}", e);
		}
		//logger.info("---获取任务结果方式FutureTask,时间差为:{}", System.currentTimeMillis() - current);
		if (executorService.isShutdown()) {
			executorService.shutdown();
		}
	}
	
	private void handleSingleFileUpload(MultipartFile file, List<String>  resultList) {
		String fileName = UploadImgUtil.uploadSingleFile(file);
		if(StringUtils.isEmpty(fileName)) {
			fileName = "";
		}
		resultList.add(fileName);
	}
}
